package com.example.traning.mall.dal.mysql.account;

import cn.hutool.core.collection.CollUtil;
import com.example.traning.mall.controller.account.vo.UserLoginInfoVO;
import com.example.traning.mall.controller.account.vo.UserLoginResultVO;
import com.example.traning.mall.dal.dataobject.account.AccountPermission;
import com.example.traning.mall.dal.dataobject.account.AccountRolePermission;
import com.example.traning.mall.dal.dataobject.account.AccountUser;
import com.example.traning.mall.dal.dataobject.account.AccountUserRole;
import com.example.traning.mall.framework.common.pojo.PageParam;
import com.example.traning.mall.framework.common.pojo.PageResult;
import com.example.traning.mall.framework.mybatis.core.mapper.BaseMapperX;
import com.example.traning.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.example.traning.mall.framework.mybatis.core.query.MPJLambdaWrapperX;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * <p>
 * 用户 Mapper 接口
 * </p>
 *
 * @author Lei Yu
 * @since 2025-04-29
 */
@Mapper
public interface AccountUserMapper extends BaseMapperX<AccountUser> {

    /**
     * 根据用户名查询用户的登录信息
     *
     * @param username 用户名
     * @return 匹配的用户的登录信息，如果没有匹配的数据，则返回null
     */
    default UserLoginInfoVO getLoginInfoByUsername(String username) {
        /*
        SELECT t.id, t.username, t.password, t.avatar, t.enable, t.login_count, t3.value
        FROM account_user t
                 LEFT JOIN account_user_role t1 ON (t1.user_id = t.id)
                 LEFT JOIN account_role_permission t2 ON (t2.role_id = t1.role_id)
                 LEFT JOIN account_permission t3 ON (t3.id = t2.permission_id)
        WHERE (t.username = ?);
        */
        MPJLambdaWrapperX<AccountUser> query = new MPJLambdaWrapperX<>();
        // select t.id, t.username, t.password, t.avatar, t.enable, t.login_count
        query.select(AccountUser::getId, AccountUser::getUsername, AccountUser::getPassword,
                AccountUser::getAvatar, AccountUser::getEnable, AccountUser::getLoginCount);
        // select t3.value
        query.select(AccountPermission::getValue);
        // left join account_user_role on account_user_role.user_id=account_user.id
        query.leftJoin(AccountUserRole.class, AccountUserRole::getUserId, AccountUser::getId);
        // left join account_role_permission on account_role_permission.role_id=account_user_role.role_id
        query.leftJoin(AccountRolePermission.class, AccountRolePermission::getRoleId, AccountUserRole::getRoleId);
        // left join account_permission on account_permission.id=account_role_permission.permission_id
        query.leftJoin(AccountPermission.class, AccountPermission::getId, AccountRolePermission::getPermissionId);

        // where username=?
        query.eq(AccountUser::getUsername, username);

        List<UserLoginInfoVO> data = selectJoinList(UserLoginInfoVO.class, query);
        if (CollUtil.isEmpty(data)) {
            return null;
        }
        UserLoginInfoVO vo = data.stream().findFirst().orElse(new UserLoginInfoVO());
        List<String> permissions = data.stream().map(UserLoginInfoVO::getValue).toList();
        return vo.setPermissions(permissions).setValue(null);

    }

    default AccountUser selectByUsername(String username) {
        // select * from account_user where username = username
        return selectOne(AccountUser::getUsername, username);
    }

    default AccountUser selectByMobile(String mobile) {
        return selectOne(AccountUser::getPhone, mobile);
    }

    default AccountUser selectByEmail(String email) {
        return selectOne(AccountUser::getEmail, email);
    }

    default PageResult<AccountUser> selectPage(PageParam pageVO) {
        return selectPage(pageVO, new LambdaQueryWrapperX<AccountUser>()
                .orderByDesc(AccountUser::getId)
        );
    }

}
